package com.qst.yunpan.controller.customer;

import com.qst.yunpan.entity.Result;
import com.qst.yunpan.entity.ShareFile;
import com.qst.yunpan.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ShareController {
    @Autowired
    private ShareService shareService;

    //文件/文件夹分享
    @RequestMapping("/shareFile.action")
    public @ResponseBody Result<String> shareFile(HttpServletRequest request, String currentPath, String[] shareFile) throws Exception{
        try {
            String shareUrl = shareService.shareFile(request,currentPath,shareFile);
            Result<String> result = new Result<>(405, true, "分享成功");
            result.setData(shareUrl);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(361, false, "分享失败");
        }
    }

    //接收分享访问
    @RequestMapping("/share.action")
    public String share(HttpServletRequest request, String shareUrl ){
        try {
            List<ShareFile> files = shareService.findShare(request,shareUrl);
            request.setAttribute("files",files);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "share";//
    }

    //查询用户分享的连接
    @RequestMapping("/searchShare.action")
    public @ResponseBody Result<List<ShareFile>> searchShare(HttpServletRequest request, byte status){
        try {
            List<ShareFile> files = shareService.findShareByName(request,status);
            Result<List<ShareFile>> result = new Result<>(415, true, "获取成功");
            result.setData(files);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(411, false, "获取失败");
        }
    }

    //取消连接
    @RequestMapping("/cancelShare.action")
    public @ResponseBody Result<String> cancelShare(String url, String filePath, byte status) throws Exception{
        System.out.println("控制层的三个参数传递"+url+filePath+status);
        try {
            String msg = shareService.cancelShare(url,filePath,status);
            Result<String> result = new Result<>(425, true, msg);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(421, false, "删除失败");
        }
    }

}
